package com.michael.leetcode;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class RomanToInt_13 {

    private static Map<String, Integer> baseMap = new HashMap<>();

    static {
        baseMap.put("I", 1);
        baseMap.put("V", 5);
        baseMap.put("X", 10);
        baseMap.put("L", 50);
        baseMap.put("C", 100);
        baseMap.put("D", 500);
        baseMap.put("M", 1000);
        baseMap.put("IV", 4);
        baseMap.put("IX", 9);
        baseMap.put("XL", 40);
        baseMap.put("XC", 90);
        baseMap.put("CD", 400);
        baseMap.put("CM", 900);
    }

    public int romanToInt(String s) {
        String[] charArray = s.split("");
        List<String> rom = new ArrayList<>();
        int k = 1;
        int i = 0;
        while (i < charArray.length) {
            if (k >= charArray.length) {
                rom.add(charArray[i]);
                break;
            }

            if (baseMap.get(charArray[i]) >= baseMap.get(charArray[k])) {
                rom.add(charArray[i]);
                k++;
                i++;
            } else {
                rom.add(charArray[i] + charArray[k]);
                i = i + 2;
                k = k + 2;
            }
        }


        int sum = 0;
        for (String v : rom) {
            int i1 = baseMap.get(v) != null ? baseMap.get(v) : 0;
            sum = sum + i1;
        }
        return sum;
    }

    @Test
    public void test() {
        String s = "MCMXCIV";
        romanToInt(s);
    }
}
